package com.example.leetdemo.finash;

public class Solution807 {

    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] xMax = new int[grid.length];
        int[] yMax = new int[grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            int max = 0;
            for (int j = 0; j < grid[0].length; j++) {
                max = Math.max(max, grid[i][j]);
            }
            xMax[i] = max;
        }
        for (int i = 0; i < grid[0].length; i++) {
            int max = 0;
            for (int j = 0; j < grid.length; j++) {
                max = Math.max(max, grid[j][i]);
            }
            yMax[i] = max;
        }
        int result = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                result += Math.min(xMax[i], yMax[j]) - grid[i][j];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Solution807 solution807 = new Solution807();
        System.err.println(solution807.maxIncreaseKeepingSkyline(new int[][]{
                {3,0,8,4},
                {2,4,5,7},
                {9,2,6,3},
                {0,3,1,0}
        }));
    }
}
